﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UI_view_FoundImages : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
            DataBind();
    }

    public override void DataBind()
    {
        if (Request["TrailID"] != null && Request["ImageID"] != null)
        {
            try
            {
                int trailId = 0;
                int imageId = 0;

                if (int.TryParse(Request["TrailID"], out trailId) && int.TryParse(Request["ImageID"], out imageId) && trailId > 0 && imageId > 0)
                {
                    IList<Bintje.Trail> trails;
                    IList<Bintje.WayPointWrapper> waypoints;
                    Bintje.ImageFilter filter = new Bintje.ImageFilter(Session["Session"] as Bintje.bintje.imageoSession);
                    filter.Id = imageId;
                    filter.InnerRadius = 1;
                    filter.OuterRadius = 100;
                    filter.MaximumNumber = 20;
                    filter.Search(out trails, out waypoints);

                    IList<Bintje.ImageWrapper> imgWrappers = new List<Bintje.ImageWrapper>();
                    for (int i = 0; i < waypoints.Count; i++)
                    {
                        if (null != waypoints[i].Images)
                            for (int j = 0; j < waypoints[i].Images.Count; j++)
                            {
                                imgWrappers.Add(waypoints[i].Images[j]);
                            }
                    }

                    var myImages = (from im in imgWrappers
                                    orderby im.Id
                                    select new
                                    {
                                        ID = im.Id,
                                        Name = waypoints.Single(w => w.Images.Single(i => i.Id == im.Id).Id == im.Id).WayPoint.Description,
                                        LocationLatitude = im.Latitude.ToString(),
                                        LocationLongitude = im.Longitude.ToString(),
                                        LocationCountry = waypoints.Single(w => w.Images.Single(i => i.Id == im.Id).Id == im.Id).WayPoint.Country,
                                        ImageUrl = im.Url
                                    }).ToArray();
                    if (myImages.Length > 0)
                    {
                        ltrHeading.Text = "Found Images";
                        rptrFoundImages.DataSource = myImages;
                        rptrFoundImages.DataBind();
                        lbltrails.Visible = false;
                    }
                    else
                    {
                        lbltrails.Text = "no Images found";
                        lbltrails.Visible = true;
                        ltrHeading.Visible = false;
                    }
                }
            }
            catch (Exception) { }
        }
        base.DataBind();
    }
}
